package com.shop.cloud.mall.api.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.cloud.mall.common.constant.MallConstants;
import com.shop.cloud.mall.common.constant.MallReturnCode;
import com.shop.cloud.mall.common.entity.CouponUser;
import com.shop.cloud.mall.common.entity.GoodsSpu;
import com.shop.cloud.mall.common.entity.UserCollect;
import com.shop.cloud.mall.common.entity.UserFootprint;
import com.shop.cloud.common.core.constant.CommonConstants;
import com.shop.cloud.common.core.util.R;
import com.shop.cloud.common.portal.util.ThirdSessionHolder;
import com.shop.cloud.mall.api.mapper.UserCollectMapper;
import com.shop.cloud.mall.api.service.CouponUserService;
import com.shop.cloud.mall.api.service.GoodsSpuService;
import com.shop.cloud.mall.api.service.UserFootprintService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品api
 *
 * @author
 * @date 2019-08-12 16:25:10
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/goodsspu")
@Tag(description = "goodsspu", name = "商品接口API")
public class GoodsSpuController {

    private final GoodsSpuService goodsSpuService;
    private final UserCollectMapper userCollectMapper;
    private final CouponUserService couponUserService;
    private final UserFootprintService userFootprintService;

    /**
     * 分页查询
     *
     * @param page     分页对象
     * @param goodsSpu spu商品
     * @return
     */
    @Operation(summary = "分页查询")
    @GetMapping("/page")
    public R getGoodsSpuPage(Page page, GoodsSpu goodsSpu, String couponUserId) {
        goodsSpu.setShelf(CommonConstants.YES);
        CouponUser couponUser = null;
        if (StrUtil.isNotBlank(couponUserId)) {
            couponUser = couponUserService.getById(couponUserId);
        }
        return R.ok(goodsSpuService.page2(page, goodsSpu, couponUser));
    }

    /**
     * 通过id查询spu商品
     *
     * @param id
     * @return R
     */
    @Operation(summary = "通过id查询spu商品")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") String id) {
        GoodsSpu goodsSpu = goodsSpuService.getById2(id);
        if (goodsSpu == null) {
            return R.failed(MallReturnCode.ERR_80004.getCode(), MallReturnCode.ERR_80004.getMsg());
        }
        String userId = ThirdSessionHolder.getUserId();
        if (StrUtil.isNotBlank(userId)) {
            //查询用户是否收藏
            UserCollect userCollect = new UserCollect();
            userCollect.setUserId(userId);
            userCollect.setType(MallConstants.COLLECT_TYPE_1);
            userCollect.setRelationId(id);
            goodsSpu.setCollectId(userCollectMapper.selectCollectId(userCollect));
            //加入足迹
            UserFootprint userFootprint = new UserFootprint();
            userFootprint.setRelationId(id);
            userFootprint.setUserId(userId);
            userFootprint.setShopId(goodsSpu.getShopId());
            userFootprintService.save(userFootprint);
        }
        return R.ok(goodsSpu);
    }

    /**
     * list查询通过ids
     *
     * @param ids ids
     * @return
     */
    @Operation(summary = "list查询通过ids")
    @PostMapping("/listbyids")
    public R getGoodsSpuListByIds(@RequestBody List<String> ids) {
        return R.ok(goodsSpuService.listGoodsSpuByIds(ids));
    }
}
